package com.itboye.mengwu.app.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.AddressQueryModel;

public class LocationAdapter extends BaseAdapter {

	private Context context;
	private List<AddressQueryModel> list;

	public LocationAdapter(Context context, List<AddressQueryModel> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	final class ViewHolder {
		TextView nametext;
		TextView phonetext;
		TextView detailtext;
		TextView uselesstext;
		RelativeLayout default_bg;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_location_listview, null);
			viewHolder = new ViewHolder();

			// 注释 by young
			viewHolder.default_bg = (RelativeLayout) convertView
					.findViewById(R.id.default_bg);
			viewHolder.nametext = (TextView) convertView
					.findViewById(R.id.nametext);
			viewHolder.phonetext = (TextView) convertView
					.findViewById(R.id.phonetext);
			viewHolder.detailtext = (TextView) convertView
					.findViewById(R.id.detailtext);
			viewHolder.uselesstext = (TextView) convertView
					.findViewById(R.id.useless);

			viewHolder.nametext.setText(list.get(position).getContactname());
			viewHolder.phonetext.setText(list.get(position).getMobile());
			String province = list.get(position).getProvince();
			String city = list.get(position).getCity();
			String area = list.get(position).getArea();
			if (0 == list.get(position).getDefault()) {

			} else {
				viewHolder.default_bg.setVisibility(View.VISIBLE);
			}
			if (city.equals("市辖区") | city.equals("县")) {
				city = "";
			}
			if (area.equals("市辖区")) {
				area = "";
			}
			String detail = list.get(position).getDetailinfo();
			viewHolder.detailtext.setText(province + city + area + detail);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		return convertView;
	}

}
